<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller;
use think\Controller;
use GatewayClient\Gateway;

/**
 * Description of Chat
 *
 * @author Rung-PC
 */
class Chat extends Controller{
    //put your code here
    public function saveMsg(){
        if(request()->isAjax()){
            $msgContent = json_decode(input('msg'),TRUE);
            $data['fromid'] =$msgContent['fromid'];
            $data['from_name'] = $this->getNick($data['fromid']);
            $data['toid'] =$msgContent['toid'];
            $data['to_name'] = $this->getNick($data['toid']);
            $data['content'] =$msgContent['content'];
            $data['is_read'] =$msgContent['is_read'];
            $data['type'] =0;//0文本 1图片
            $data['time'] =$msgContent['time'];
            
            db('msg')->insert($data);
        }
    }
    
    public function getNick($uid){
        return db('users')->where('id',$uid)->value('nick_name');
    }
    
    public function getHead(){
        if(request()->isAjax()){
            $fromHead = db('users')->where('id', input('fromid'))->value('head_img');
            $toHead = db('users')->where('id', input('toid'))->value('head_img');
            return ['fromHead'=>$fromHead,'toHead'=>$toHead];
        }
    }
    
    public function getNickAjax(){
        if(request()->isAjax()){
            $nickName = db('users')->where('id',input('toid'))->value('nick_name');
            $nick = strlen($nickName) > 10 ? substr($nickName,0,10).'...' : $nickName;
            return ['nickName'=>$nick];
        }
    }
    
    public function msgLoad(){
        if(request()->isAjax()){
            $fromId = input('fromid');
            $toId = input('toid');
            $msgCount = db('msg')->where("(fromid = ? AND toid = ?) OR (fromid = ? AND toid = ?)",[$fromId,$toId,$toId,$fromId])->count('id');
            $start = $msgCount > 10 ? $msgCount+10*(-1) : 0;
            $msgAry = db('msg')->where("(fromid = ? AND toid = ?) OR (fromid = ? AND toid = ?)",[$fromId,$toId,$toId,$fromId])->limit($start,10)->select();            
            if(!empty($msgAry)){
                foreach ($msgAry as $key => $value) {
                    if($value['type'] == 1){
                        $msgAry[$key]['imgPath'] = date("Ymd",$value['time']).'/'.$value['content'];
                    }
                }
            }
//            halt($msgAry);
            return ['msg'=>$msgAry];
        }
    }
    
    public function msgImg(){
        if(!empty($_FILES)){
//            var_dump(input());die;
            $file = request()->file('file');
            $uploadPath = env('root_path').'public/images/msg_img/';
            $info = $file->validate(['size'=>204800,'ext'=>'jpg,png,gif'])->move($uploadPath);
            if($info){
                $fileName = $info->getFilename();
                $imgUrl = date("Ymd").'/'.$fileName;
                
                $data['fromid'] = input('fromid');
                $data['from_name'] = $this->getNick($data['fromid']);
                $data['toid'] = input('toid');
                $data['to_name'] = $this->getNick($data['toid']);
                $data['content'] = $fileName;
                $data['is_read'] = 0;     
                $data['type'] = 1;//0文本 1图片
                $data['time'] = time();
            
                db('msg')->insert($data);
            
                return json(["status"=>1,"msg"=>"上传成功!","imgUrl"=>$imgUrl]);
            }else{
                return json(["status"=>0,"msg"=>$file->getError()]);
            }
        } 
    }
    
    public function changeMsgSta(){
        if(request()->isAjax()){
            $fromid = input('fromid');
            $toid = input('toid');
            $database = config('database.prefix').'msg';
            $result = db()->query("UPDATE $database  SET `is_read` = 1  WHERE  ( fromid = $toid AND toid = $fromid AND is_read = 0 )");            //halt(999);
            return ['status'=>1];
        }
    }
    
    public function oldMsg(){
        if(request()->isAjax()){
            $fromId = input('fromid');
            $toId = input('toid');
            $curpage = empty(input('curpage')) ? 2 : input('curpage');
            $pagesize = 10;
            
            $msgCount = db('msg')->where("(fromid = ? AND toid = ?) OR (fromid = ? AND toid = ?)",[$fromId,$toId,$toId,$fromId])->count('id');
            $pagenum = ceil($msgCount/$pagesize);   //dump($pagenum);
            
            if($curpage <= $pagenum){
                $start = $msgCount > 10 ? ($msgCount+$pagesize*(-$curpage) < 0 ? 0:$msgCount+$pagesize*(-$curpage)) : 0;            //dump($start);
                $msgAry = db('msg')->where("(fromid = ? AND toid = ?) OR (fromid = ? AND toid = ?)",[$fromId,$toId,$toId,$fromId])->limit($start,$pagesize)->select();            
                if(!empty($msgAry)){
                    foreach ($msgAry as $key => $value) {
                        if($value['type'] == 1){
                            $msgAry[$key]['imgPath'] = date("Ymd",$value['time']).'/'.$value['content'];
                        }
                    }
                     return ["status"=>1,"msg"=>$msgAry,"start"=>$start,"msgCount"=>$msgCount,"pagenum"=>$pagenum,"curpage"=>$curpage];
                }
            }
                return ["status"=>0,"msg"=>"没有更多记录了!"];
        }
    }
}
